#!/usr/bin/python
# -*- coding: utf-8 -*-

"""Project Euler Solution 040

Copyright (c) 2011 by Robert Vella - robert.r.h.vella@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

import math
from euler.numbers.decimal_base import integer_to_digits
from euler.list_functions import true_product
import cProfile

def get_answer():    
    """Question:
    
    An irrational decimal fraction is created by concatenating the positive 
    integers:
    
    0.123456789101112131415161718192021...
    
    It can be seen that the 12th digit of the fractional part is 1.
    
    If dn represents the nth digit of the fractional part, find the value of 
    the following expression.
    
    d1 × d10 × d100 × d1000 × d10000 × d100000 × d1000000
    """
    
    def get_num_at_n(n):
        """Return the [n]th integer in the fraction specified by this problem."""
        
        #The index of the power of 10 before index n.
        prev_power_of_10_index = 0
        
        #The index of the power of 10 after index n.
        next_power_of_10_index = 9
        
        #The exponent of the previous power of 10.
        power = 1
        
        #Keep going through all the powers of 10 until the index of 10 which
        #is less than index n is maximised.
        while n >= next_power_of_10_index:        
            power += 1
            
            prev_power_of_10_index = next_power_of_10_index             
            next_power_of_10_index = next_power_of_10_index \
                                + (10 ** power - 10 ** (power - 1)) * power
        
        #The distance between index n and the power of 10 right before it. 
        distance_n_power_of_10 = (n - prev_power_of_10_index)
        
        #The concatinated integer at index n.
        num_at_n = 10 ** (power - 1) + math.floor(distance_n_power_of_10 / power)    
        
        #Return the digit at index n.
        return list(integer_to_digits(num_at_n))[distance_n_power_of_10 % power]
    
    #Return result.
    return true_product(get_num_at_n(10 ** x - 1) for x in range(7))

if __name__ == "__main__":
    cProfile.run("print(get_answer())")
